/**
 * 
 */
package trungsi.gea.photos.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

import org.json.JSONArray;

import trungsi.gea.photos.filter.FacebookClientHolder;

import com.google.code.facebookapi.FacebookJsonRestClient;

/**
 * @author trungsi
 *
 */
@UrlBinding("/fb/")
public class FacebookActionBean extends BaseActionBean {

	private FacebookJsonRestClient client;
	
	private JSONArray friends;
	
	@Before
	public void before() {
		client = FacebookClientHolder.getFacebookClient(); 
	}
	
	@DefaultHandler
	public Resolution index() throws Exception {
		friends = client.friends_get();
		List<Long> userIds = getFriendIds();
		Set<CharSequence> fields = new HashSet<CharSequence>();
		fields.add("uid");fields.add("first_name");fields.add("last_name");
		System.out.println(client.users_getStandardInfo(userIds, fields));
		System.out.println(client.users_getLoggedInUser());
		userIds.add(client.users_getLoggedInUser());
		System.out.println(client.users_getStandardInfo(userIds, fields));
		getContext().getRequest().getSession().setAttribute("facebookUser", client.users_getStandardInfo(userIds, fields));
		return new ForwardResolution("/fb/friends.jsp");
	}
	
	private List<Long> getFriendIds() {
		System.out.println(friends);
		ArrayList<Long> ids = new ArrayList<Long>();
		try {
			for (int i = 0; i < friends.length(); i++) {
				ids.add(friends.getLong(i));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(ids);
		return ids;
	}

	public JSONArray getFriends() {
		return friends;
	}
}
